package model.player;

import java.util.LinkedList;
import java.util.List;

public class StructureTypeManager implements SubTypeManager
{
	private List<StructureManager> structureManagers;
	
	private int smPtr;
	
	public StructureTypeManager()
	{
		structureManagers = new LinkedList<StructureManager>();
		
		smPtr = 0;
	}
	
	public void addStructureManager(StructureManager sm)
	{
		structureManagers.add(sm);
	}
	
	@Override
	public CyclableManager getCurSubType() 
	{
		System.out.println("Structure buh");
		if (!hasStructures())
			return null;
		
		System.out.println("Structure muh");
		while (structureManagers.get(smPtr) == null || structureManagers.get(smPtr).isEmpty())
		{
			smPtr = (smPtr + 1) % structureManagers.size();
		}
		
		return structureManagers.get(smPtr);
	}

	@Override
	public CyclableManager getNextSubType() 
	{
		if (!hasStructures())
			return null;
		
		smPtr = (smPtr + 1) % structureManagers.size();
		
		while (structureManagers.get(smPtr) == null || structureManagers.get(smPtr).isEmpty())
		{
			smPtr = (smPtr + 1) % structureManagers.size();
		}
		
		return structureManagers.get(smPtr);
	}

	@Override
	public CyclableManager getPrevSubType() 
	{
		if (!hasStructures())
			return null;
		
		smPtr = (smPtr - 1 + structureManagers.size()) % structureManagers.size();
		
		while (structureManagers.get(smPtr) == null || structureManagers.get(smPtr).isEmpty())
		{
			smPtr = (smPtr - 1 + structureManagers.size()) % structureManagers.size();
		}
		
		return structureManagers.get(smPtr);
	}

	public boolean hasStructures() 
	{
		for (StructureManager sm : structureManagers)
			if (!sm.isEmpty())
				return true;
		
		return false;
	}
}
